<!--

    Copyright © 2016-2023 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form #formContainer class="tb-multiple-input"
      [formGroup]="multipleInputFormGroup"
      tb-toast toastTarget="{{ toastTargetId }}"
      (ngSubmit)="saveForm()" novalidate autocomplete="off">
  <div class="tb-multiple-input-container" *ngIf="entityDetected && isAllParametersValid">
    <fieldset *ngFor="let source of sources" [ngClass]="{'fields-group': settings.showGroupTitle}">
      <legend class="group-title" *ngIf="settings.showGroupTitle">{{ getGroupTitle(source.datasource) }}
      </legend>
      <div fxLayout="row" class="layout-wrap"
           [ngClass]="{'vertical-alignment': isVerticalAlignment || changeAlignment}">
        <div *ngFor="let key of visibleKeys(source)"
             [ngStyle]="{width: (isVerticalAlignment || changeAlignment) ? '100%' : inputWidthSettings}">
          <div class="input-field" *ngIf="key.settings.dataKeyValueType === 'string'">
            <mat-form-field class="mat-block">
              <mat-label>{{key.label}}</mat-label>
              <input matInput
                     formControlName="{{key.formId}}"
                     [required]="key.settings.required"
                     [readonly]="key.settings.isEditable === 'readonly'"
                     type="text"
                     (focus)="key.isFocused = true; focusInputElement($event)"
                     (blur)="key.isFocused = false; inputChanged(source, key)">
              <ng-container *ngIf="key.settings.icon || key.settings.safeCustomIcon" matPrefix>
                <mat-icon *ngIf="!key.settings.safeCustomIcon; else customToggleIcon">{{key.settings.icon}}</mat-icon>
                <ng-template #customToggleIcon>
                  <img class="mat-icon" [src]="key.settings.safeCustomIcon" alt="icon">
                </ng-template>
              </ng-container>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('required')">
                {{ getErrorMessageText(key.settings, 'required') }}
              </mat-error>
            </mat-form-field>
          </div>
          <div class="input-field" *ngIf="key.settings.dataKeyValueType === 'double' ||
                                          key.settings.dataKeyValueType === 'integer'">
            <mat-form-field class="mat-block">
              <mat-label>{{key.label}}</mat-label>
              <input matInput
                     formControlName="{{key.formId}}"
                     [required]="key.settings.required"
                     [readonly]="key.settings.isEditable === 'readonly'"
                     type="number"
                     step="{{key.settings.step}}"
                     min="{{key.settings.minValue}}"
                     max="{{key.settings.maxValue}}"
                     (focus)="key.isFocused = true; focusInputElement($event)"
                     (blur)="key.isFocused = false; inputChanged(source, key)">
              <ng-container *ngIf="key.settings.icon || key.settings.safeCustomIcon" matPrefix>
                <mat-icon *ngIf="!key.settings.safeCustomIcon; else customToggleIcon">{{key.settings.icon}}</mat-icon>
                <ng-template #customToggleIcon>
                  <img class="mat-icon" [src]="key.settings.safeCustomIcon" alt="icon">
                </ng-template>
              </ng-container>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('required')">
                {{ getErrorMessageText(key.settings,'required') }}
              </mat-error>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('min')">
                {{ getErrorMessageText(key.settings,'min') }}
              </mat-error>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('max')">
                {{ getErrorMessageText(key.settings,'max') }}
              </mat-error>
            </mat-form-field>
          </div>
          <div class="input-field mat-block" *ngIf="key.settings.dataKeyValueType === 'booleanCheckbox'">
            <mat-checkbox formControlName="{{key.formId}}"
                          (change)="inputChanged(source, key)">
              <span class="label-wrapper">{{key.label}}</span>
            </mat-checkbox>
          </div>
          <div class="input-field" *ngIf="key.settings.dataKeyValueType === 'JSON'">
            <mat-form-field class="mat-block">
              <mat-label>{{key.label}}</mat-label>
              <input
                matInput
                type="text"
                formControlName="{{key.formId}}"
                tb-json-to-string
                [readonly]="key.settings.isEditable === 'readonly'"
                [required]="key.settings.required"
                (focus)="key.isFocused = true; focusInputElement($event)"
                (blur)="key.isFocused = false; inputChanged(source, key)"
              />
              <ng-container *ngIf="key.settings.icon || key.settings.safeCustomIcon" matPrefix>
                <mat-icon *ngIf="!key.settings.safeCustomIcon; else customToggleIcon">{{key.settings.icon}}</mat-icon>
                <ng-template #customToggleIcon>
                  <img class="mat-icon" [src]="key.settings.safeCustomIcon" alt="icon">
                </ng-template>
              </ng-container>
              <button [disabled]="key.settings.isEditable === 'disabled' || key.settings.isEditable === 'readonly'"
                      type="button"
                      matSuffix mat-icon-button
                      (click)="openEditJSONDialog($event, key, source)">
                <mat-icon>open_in_new</mat-icon>
              </button>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('required')">
                {{ getErrorMessageText(key.settings,'required') }}
              </mat-error>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('invalidJSON')">
                {{ getErrorMessageText(key.settings,'invalidJSON') | translate }}
              </mat-error>
            </mat-form-field>
          </div>
          <div class="input-field mat-block" *ngIf="key.settings.dataKeyValueType === 'booleanSwitch'">
            <mat-slide-toggle formControlName="{{key.formId}}"
                              [labelPosition]="key.settings.slideToggleLabelPosition"
                              (change)="inputChanged(source, key)">
              <ng-container *ngIf="key.settings.icon || key.settings.safeCustomIcon">
                <mat-icon *ngIf="!key.settings.safeCustomIcon; else customToggleIcon">{{key.settings.icon}}</mat-icon>
                <ng-template #customToggleIcon>
                  <img class="mat-icon" [src]="key.settings.safeCustomIcon" alt="icon">
                </ng-template>
              </ng-container>
              <span class="label-wrapper">{{key.label}}</span>
            </mat-slide-toggle>
          </div>
          <div class="input-field mat-block date-time-input" *ngIf="(key.settings.dataKeyValueType === 'dateTime') ||
                                                    (key.settings.dataKeyValueType === 'date') ||
                                                    (key.settings.dataKeyValueType === 'time')" fxLayout="column">
            <mat-form-field>
              <mat-label>{{key.label}}</mat-label>
              <mat-datetimepicker-toggle [for]="datePicker" matPrefix></mat-datetimepicker-toggle>
              <mat-datetimepicker #datePicker type="{{datePickerType(key.settings.dataKeyValueType)}}"
                                  openOnFocus="true"></mat-datetimepicker>
              <input matInput formControlName="{{key.formId}}"
                     [required]="key.settings.required"
                     [readonly]="key.settings.isEditable === 'readonly'"
                     [matDatetimepicker]="datePicker"
                     (focus)="key.isFocused = true;"
                     (blur)="key.isFocused = false;"
                     (dateChange)="inputChanged(source, key)">
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('required')">
                {{ getErrorMessageText(key.settings, 'required') }}
              </mat-error>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('matDatepickerParse')">
                {{ getErrorMessageText(key.settings, 'invalidDate') }}
              </mat-error>
            </mat-form-field>
          </div>
          <div class="input-field" *ngIf="key.settings.dataKeyValueType === 'select'">
            <mat-form-field class="mat-block">
              <mat-label>{{key.label}}</mat-label>
              <mat-select formControlName="{{key.formId}}"
                          [required]="key.settings.required"
                          (focus)="key.isFocused = true;"
                          (selectionChange)="key.isFocused = false; inputChanged(source, key)">
                <mat-option *ngFor="let option of key.settings.selectOptions"
                            [value]="option.value"
                            [disabled]="key.settings.isEditable === 'readonly'">
                  {{ getCustomTranslationText(option.label ? option.label : option.value) }}
                </mat-option>
              </mat-select>
              <mat-error *ngIf="multipleInputFormGroup.get(key.formId).hasError('required')">
                {{ getErrorMessageText(key.settings, 'required') }}
              </mat-error>
            </mat-form-field>
          </div>
        </div>
      </div>
    </fieldset>
  </div>
  <div class="mat-padding" fxLayout="row" fxLayoutAlign="end center"
       *ngIf="entityDetected && isAllParametersValid && settings.showActionButtons">
    <button mat-button color="primary" type="button"
            (click)="discardAll()" style="max-height: 50px; margin-right:20px;"
            [disabled]="!multipleInputFormGroup.dirty">
      {{ resetButtonLabel }}
    </button>
    <button mat-button mat-raised-button color="primary" type="submit"
            style="max-height: 50px; margin-right:20px;"
            [disabled]="!multipleInputFormGroup.dirty || multipleInputFormGroup.invalid">
      {{ saveButtonLabel }}
    </button>
  </div>
  <div class="tb-multiple-input__errors" fxLayout="column" fxLayoutAlign="center center" style="height: 100%;"
       *ngIf="(!entityDetected || !isAllParametersValid) && datasourceDetected">
    <div style="text-align: center; font-size: 18px; color: #a0a0a0;" [fxHide]="entityDetected">
      {{ 'widgets.input-widgets.no-entity-selected' | translate }}
    </div>
    <div style="text-align: center; font-size: 18px; color: #a0a0a0;"
         [fxShow]="entityDetected && !isAllParametersValid">
      {{ 'widgets.input-widgets.not-allowed-entity' | translate }}
    </div>
  </div>
</form>
